<template>
  <div class="work-calendar">
    <div class="select-box">
      <el-select
        v-model="currentYear"
        placeholder=""
        size="small"
        style="width: 120px; margin-right: 10px"
        @change="changeDate"
      >
        <el-option v-for="v in yearList" :key="v" :label="v" :value="v"> </el-option>
      </el-select>
      <el-select
        v-model="currentMonth"
        placeholder=""
        size="small"
        style="width: 120px;"
        @change="changeDate"
      >
        <el-option v-for="v in 12" :key="v" :label="v" :value="v"> </el-option>
      </el-select>
      <el-button
        type="default"
        size="small"
        style="margin-left:10px"
        @click="currentDate = new Date()"
      >
        今天
      </el-button>
    </div>
    <el-calendar v-model="currentDate">
      <template #dateCell="{ data,date }">
        <div class="date-content">
          <span class="text">{{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  name: 'WorkCalendar',
  props: {
    startDate: {
      type: Date,
      default: () => {
        return new Date()
      },
    },
  },
  data() {
    return {
      currentYear: null, // 当前年份
      currentMonth: null, // 当前月份
      currentDate: null,
    }
  },
  computed: {
    // 要遍历的年的数组
    yearList() {
      return Array.from(new Array(10), (item, index) => index + this.currentYear - 5)
    },
  },
  watch: {
    // 监听日历当前值的改变,回现下拉框
    currentDate(newValue) {
      // console.log(newValue)
      this.currentYear = newValue.getFullYear()
      this.currentMonth = newValue.getMonth() + 1
    },
  },
  created() {
    this.currentYear = this.startDate.getFullYear() // 得到当前年份
    this.currentMonth = this.startDate.getMonth() + 1 // 当前月份
    this.changeDate()
  },
  methods: {
    // 一、判断是否是周末
    isWeek(date) {
      // console.log(date)
      // console.log(data)
      return date.getDay() === 6 || date.getDay() === 0
    },

    // 二、日历的change事件
    changeDate() {
      // console.log('change')
      // 生成新的日期
      const day = new Date()
      const dayStr = '-' + day.getDate()
      // console.log(dayStr)
      // console.log(day.getDate())
      this.currentDate = new Date(this.currentYear + '-' + this.currentMonth + dayStr)
    },
  },
}
</script>

<style lang="scss" scoped>
.select-box {
  display: flex;
  justify-content: flex-end;
}
::v-deep {
  .el-calendar__header {
    display: none;
  }
  .el-calendar-table td.is-selected .text {
    background: #409eff;
    color: #fff;
    border-radius: 50%;
  }
}
.el-calendar {
  text-align: center;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
</style>
